﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConfigurationManager.View.Dialog;
using System.Diagnostics;

namespace ConfigurationManager.View
{
    public partial class frmProductList : Form
    {
        List<Model.Product> products;
        public frmProductList()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmProduct_Load);
        }

        private void frmProduct_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            LoadProducts();
        }

        private void LoadProducts()
        {
            products = Model.Product.Retrieve();
            dtgProducts.DataSource = products;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadProducts();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmProduct addProduct = new frmProduct();
            addProduct.Owner = this;
            if (addProduct.ShowDialog() == DialogResult.OK)
                LoadProducts();
        }

        private void dtgProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;

            if (dtgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex] == dtgProducts.Rows[e.RowIndex].Cells["Edit"])
                EditProduct((Model.Product)dtgProducts.Rows[e.RowIndex].DataBoundItem);
            else if (dtgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex] == dtgProducts.Rows[e.RowIndex].Cells["Delete"])
                DeleteProduct((Model.Product)dtgProducts.Rows[e.RowIndex].DataBoundItem);

        }
        
        private void dtgProducts_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditProduct((Model.Product)dtgProducts.Rows[e.RowIndex].DataBoundItem);
        }

        private void EditProduct(Model.Product product)
        {
            frmProduct addProduct = new frmProduct(product);
            addProduct.Owner = this;
            if (addProduct.ShowDialog() == DialogResult.OK)
                LoadProducts();
        }

        private void DeleteProduct(Model.Product product)
        {
            try
            {
                int id;
                string name;
                id = product.Id;
                name = product.Name;
                if (MessageBox.Show(string.Format("Confirma a exclusão do produto {0} - {1}?", id, name), "Confimar Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Model.Product.Delete(id);
                    MessageBox.Show("Produto excluído com sucesso!", "Process Manager Configuration", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadProducts();
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                //EventLog.WriteEntry("Process Manager - Configuration Manager", ex.ToString(), EventLogEntryType.Error);
                //MessageBox.Show("Ocorreu um erro interno, verifique o event viewer para maiores detalhes");
                Program.HandleException(ex);
            }
        }
    }
}
